package com.lzh.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.eduservice.entity.EduChapter;
import com.lzh.eduservice.entity.EduVideo;
import com.lzh.eduservice.entity.chapter.ChapterVo;
import com.lzh.eduservice.entity.chapter.VideoVo;
import com.lzh.eduservice.mapper.EduChapterMapper;
import com.lzh.eduservice.mapper.EduVideoMapper;
import com.lzh.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.eduservice.service.EduVideoService;
import com.lzh.oss.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2020-12-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    /**
     *
     */
    @Autowired
    private EduVideoMapper eduVideoMapper;

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> queryInfo(String courseId) {
        //根据课程id查询章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterWrapper);

        //根据课程id查询小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoMapper.selectList(videoWrapper);

        List<ChapterVo> finalList = new ArrayList<>();

        eduChapters.forEach(eduChapter -> {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            List<VideoVo> videoList = new ArrayList<>();
            eduVideos.forEach(eduVideo -> {
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            });
            chapterVo.setChildren(videoList);
        });
        return finalList;
    }

    @Override
    public boolean delectChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "不能删除");
        } else {

            int i = baseMapper.deleteById(chapterId);
            return i > 0;
        }
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
